/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui.combination;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import lineage.ui.customize.L1CustomizeJList;
import lineage.ui.customize.L1CustomizeJMenuItem;
import lineage.ui.customize.L1CustomizeScrollPane;
import lineage.util.Util;

/**
 * 列表框.
 * 
 * @version 2014年2月23日下午10:50:32
 * @author jrwz
 */
public class L1List extends L1CustomizeScrollPane implements MouseListener, ActionListener {
    private static final long serialVersionUID = -8889387474053085709L;

    protected JList<Object> list;
    protected DefaultListModel<Object> listModel;
    protected JPopupMenu jPopupMenu;
    protected JMenuItem[] popmenuitem;
    /** 字串形式的右键菜单. */
    protected String[] menuitemstr;

    protected L1List() {
    }

    @Override
    protected void init(boolean flag) {
        addListModel();
        addList();

        super.init(flag);
    }

    protected void addListModel() {
        listModel = new DefaultListModel<Object>();
    }

    protected void addList() {
        list = new L1CustomizeJList(listModel);
        list.add(addMyJPopupMenu()); // 增加右键菜单
        list.addMouseListener(this); // 增加鼠标监听器
        setListCellRenderer();
        setViewportView(list);
    }

    /**
     * 设置用于绘制列表中每个单元的委托.
     */
    protected void setListCellRenderer() {
        list.setCellRenderer(new L1ListCellRenderer());
    }

    /**
     * 通过此方法向列表内增加或删除元素.
     */
    public DefaultListModel<Object> getDLM() {
        return listModel;
    }

    /**
     * 右键菜单.
     */
    protected JPopupMenu addMyJPopupMenu() {
        jPopupMenu = new JPopupMenu();
        popmenuitem = new L1CustomizeJMenuItem[menuitemstr.length];
        for (int i = 0; i < popmenuitem.length; i++) {
            popmenuitem[i] = new L1CustomizeJMenuItem(menuitemstr[i]); // 菜单项
            jPopupMenu.add(popmenuitem[i]); // 快捷菜单项加入弹出菜单
            popmenuitem[i].addActionListener(this); // 增加鼠标右键菜单动作监听器
        }
        return jPopupMenu;
    }

    /**
     * 当鼠标点击时(按下并释放)调用.
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        // 右键可以选中
        int index = list.locationToIndex(e.getPoint());
        list.setSelectedIndex(index);
        // 右键单击
        if (e.getModifiers() == MouseEvent.BUTTON3_MASK && list.getSelectedIndex() != -1) {
            // 显示菜单
            jPopupMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }

    /**
     * 当鼠标按下时调用.
     */
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO 自动生成的方法存根

    }

    /**
     * 当鼠标释放时调用.
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO 自动生成的方法存根

    }

    /**
     * 当鼠标进入时调用.
     */
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO 自动生成的方法存根

    }

    /**
     * 当鼠标离开时调用.
     */
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO 自动生成的方法存根

    }

    /**
     * 当一个动作发生时调用.
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        // 鼠标左键点击
        if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
            // String str = e.getActionCommand();
            // Util.message("" + popmenuitem[0].getText());
            Object selected = list.getModel().getElementAt(list.getSelectedIndex());
            Util.message("你点了:" + selected);
        }
    }
}